<template >
  <div>
    <brand></brand>

    <el-card class="Hys_card">
      <el-button type="success" @click="dialogVisible = true">添加用户</el-button>

      <el-table :data="rolelist" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item1,i1) in scope.row.children"
              :key="item1.id"
              :class="['bdbottom',i1===0 ? 'bdtop' : '']"
            >
              <!-- //渲染一级权限 -->
              <el-col :span="5 ">
                <el-tag closable @close="removeRigthById(scope.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- //渲染二,三级权限 -->
              <el-col :span="19">
                <!-- 通过for循环 嵌套渲染二级权限 -->
                <el-row
                  v-for="(item2,i2) in item1.children"
                  :key="item2.id"
                  :class="[i2===0 ? 'bdtop':'']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRigthById(scope.row,item2.id)"
                    >{{item2.authName}}</el-tag>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      closable
                      @close="removeRigthById(scope.row,item3.id)"
                      type="warning"
                      v-for="(item3) in item2.children"
                      :key="item3.id"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="商品id"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template >
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
            <el-button
              type="warning"
              icon="el-icon-star-off"
              size="mini"
              @click="showSetRightDialog"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 对话框 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="50%" @closed="reset1">
        <el-form :model="rolesFrom" :rules="rules" ref="rolesFrom" label-width="100px">
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="rolesFrom.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色地址" prop="roleDesc">
            <el-input v-model="rolesFrom.roleDesc"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirms">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分配权限 -->
      <el-dialog
        title="分配权限"
        :visible.sync="SetRightDialogdialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-tree
          :data="rightsList"
          :props="defaultProps"
          show-checkbox
          node-key="id"
          default-expand-all:default-checked-keys="defKeys"
        ></el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="SetRightDialogdialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import brand from "../Bread";
export default {
  components: {
    brand
  },
  data() {
    return {
      rolelist: [],
      dialogVisible: false,
      SetRightDialogdialogVisible: false,
      rightsList: [],

      defKeys: [105, 116],

      defaultProps: {
        children: "children",
        label: "authName"
      },
      rolesFrom: {
        roleName: "",
        roleDesc: ""
      },
      rules: {
        roleName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        roleDesc: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.rolelis();
  },
  methods: {
    async rolelis() {
      let res = await this.$http.get("/roles");
      let { data, meta } = res.data;
      if (meta.status == 200) {
        this.rolelist = data;
      } else {
        return;
      }
    },
    async confirms() {
      this.$refs.rolesFrom.validate(async valid => {
        if (!valid) return false;
        let res = await this.$http.post("roles", this.rolesFrom);
        console.log(res);
        let { data, meta } = res.data;
        if (meta.status == 201) {
          this.rolesFrom = data;
          this.$message.success(meta.msg);
          this.rolelis();
          this.dialogVisible = false;
        } else {
          this.$message.error(meta.msg);
        }
      });
    },
    reset1() {
      this.$refs.rolesFrom.resetFields();
    },
    async removeRigthById(role, rightId) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmResult !== "comfirm") {
        return this.$message.info("取消了删除");
      }
      const { data: res } = await this.$http.delete(
        `roles/${role.id}/rights/${rightId}`
      );
      if (res.meta.status !== 200) {
        return this.$message.error("删除失败");
      }
      this.rolelis();
    },
    async showSetRightDialog() {
      const { data: res } = await this.$http.get("rights/tree");
      if (res.meta.status !== 200) {
        return this.$message.error("获取权限失败");
      }
      this.rightsList = res.data;
      this.getLeafKeys(role, this.defKeys);
      this.SetRightDialogdialogVisible = true;
    }
  },
  async getLeafKeys(noede, arr) {
    if (!node.children) {
      return arr.push(node.id);
    }
    node.children.forEach(item => {
      this.getLeafKeys(item, arr);
    });
  }
};
</script>
<style lang="scss" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-top: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>

<template>
    <div>
            <el-card class="Hys_card">
              <!-- 点击添加用户弹出模态框 然后给这个按钮绑定个事件 将这个事件设置为ture  之后在data里定义数据对象
              更改组件里面的内容 -->
            <el-button type="success" @click="addlist">添加用户</el-button>
            </el-card>
      <el-table :data="rolesList" border style="width: 100%">
     <el-table-column type="expand">
      <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
            <div class="cont" v-for="(item,index) in props.row.children" :key="index">
              <!-- 左侧 -->
              <div class="left">
                <el-button type="primary">{{item.authName}}<i class="el-icon-close el-icon--right" @click="del(item.id,props.row.id)"></i></el-button>
              </div>
              <!-- 右侧 -->
              <div class="right" v-for="(item1,index) in item.children" :key="index">
                <div class="right_one">
                  <el-button type="success">{{item1.authName}}<i class="el-icon-close el-icon--right" @click="del(item.id,props.row.id)"></i></el-button>
                </div>
                <div class="right_two" v-for="(item2,index) in item1.children" :key="index">
                  <el-button type="warning">{{item2.authName}}<i class="el-icon-close el-icon--right" @click="del(item.id,props.row.id)"></i></el-button>
                </div>
              </div>
            </div>
        </el-form>
      </template>
    </el-table-column>
    <el-table-column
      label="商品 ID"
      prop="id">
    </el-table-column>
    <el-table-column
      label="角色名称"
      prop="roleName">
    </el-table-column>
    <el-table-column
      label="角色描述"
      prop="roleDesc">
    </el-table-column>
    <el-table-column label="操作" width="300">
          <template v-slot="scope">
            <!-- 修改按钮 -->
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="compile(scope.row)"
              >编辑</el-button
            >
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete-solid"
              size="mini"
              @click="deleteIDs(scope.row)"
              >删除</el-button
            >
            <!-- 设置按钮 -->
            <el-button size="mini" type="warning" icon="el-icon-s-tools"
              @click="show(scope.row)">分配权限</el-button
            >
          </template>
        </el-table-column>
      <!-- 模态框 -->
      <!-- 对话框 -->
      </el-table>
         <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="50%"
               >
               <!-- 内容主体区 -->
              <el-form  :model="Input" :rules="rules" ref="ruleForm" label-width="100px">
              <el-form-item label="角色名称" prop="roleName">
                <el-input v-model="Input.roleName"></el-input>
              </el-form-item>
                <el-form-item label="角色描述" prop="roleDesc">
                <el-input v-model="Input.roleDesc"></el-input>
              </el-form-item>
               </el-form>
               <!-- 内容按钮区 -->

                <span slot="footer" class="dialog-footer">
                  <el-button @click="set">取 消</el-button>
                  <el-button type="primary" @click="end">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 编辑的对话框 -->
                   <el-dialog
                title="提示"
                :visible.sync="dialogVisibles"
                width="50%"
               >
              <el-form  :model="Inputs" :rules="rules" ref="ruleForm" label-width="100px">
              <el-form-item label="角色名称" prop="roleName">
                <el-input v-model="Inputs.roleName"></el-input>
              </el-form-item>
                <el-form-item label="角色描述" prop="roleDesc">
                <el-input v-model="Inputs.roleDesc"></el-input>
              </el-form-item>
               </el-form>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisibles=false">取 消</el-button>
                  <el-button type="primary" @click="modification">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 分配权限对话款 -->
            <el-dialog
                title="提示"
                :visible.sync="dialogVisibless"
                width="30%"
               >
                    <el-tree
                    :data="data"
                    show-checkbox
                    node-key="id"
                    :default-expanded-keys="[2, 3]"
                    :default-checked-keys="[5]"
                     :props="defaultProps"
                     ref="tree"
                  >
                  </el-tree>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisibless = false">取 消</el-button>
                  <el-button type="primary" @click="queding">确 定</el-button>
                </span>
              </el-dialog>
    </div>
</template>
<script>
export default {
  data() {
    return {
      rolesList: [],
      //分配权限的数组
      data: [],
      Input: {
        roleName: "",
        roleDesc: ""
      },
      Inputs: {
        roleName: 1,
        roleDesc: 2
      },
      rules: {
        roleName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        roleDesc: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      },
      dialogVisible: false,
      dialogVisibles: false,
      dialogVisibless: false,
      defaultProps: {
        children: "children",
        label: "authName"
      },
      id: "",
      suib: []
    };
  },
  created() {
    this.addForm();
  },
  methods: {
    async addForm() {
      const res =  await this.$http.get("/roles");
      let { data, meta } = res.data;
      if (meta.status == 200) {
      console.log(meta);
        this.rolesList = data;
      }
    },
    addlist() {
      this.dialogVisible = true;
    },
    set() {
      this.$refs.ruleForm.resetFields();
      this.dialogVisible = false;
    },
    async end() {
      const res =await this.$http.post("roles", this.rolesFrom);
      let { meta } = res.data;
      if (meta.status == 201) {
        this.$message.success("创建成功");
        this.Input = {};
        this.addForm();
        this.dialogVisible = false;
      }
    },
    async compile(scope) {
      this.dialogVisibles = true;
      const res = await this.$apis.compileID(scope);
      let { data, meta } = res.data;
      if (meta.status == 200) {
        this.Inputs = data;
      }
    },
    async modification() {
      const res = await this.$apis.modificationID(this.Inputs);
      let { meta } = res.data;
      if (meta.status == 200) {
        this.$message.success("修改成功");
        this.addForm();
        this.dialogVisibles = false;
      }
    },
    async deleteIDs(scope) {
      const res = await this.$apis.deleteId(scope);
      let { meta } = res.data;
      if (meta.status == 200) {
        try {
          await this.$ask("你确定要删除嘛？");
        } catch (error) {
          console.log(error);
        }
        this.addForm();
      }
    },
    async show(scope) {
      this.dialogVisibless = true;
      //这里的ID赋值到上面的ID里
      this.id = scope.id;
      const res = await this.$apis.showID();
      let { data, meta } = res.data;
      if (meta.status == 200) {
        this.data = data;
      }
    },
    async queding() {
      //获取自己的数组父级数据id
      var aa = this.$refs.tree.getHalfCheckedKeys();
      //获取子级数据id  然后放到数组里
      this.suib = this.$refs.tree.getCheckedKeys();
      //把父级的数据ID加入到数组 合并到一起
      this.suib.push(...aa);
      //把数组的数据转换字符串
      var bb = this.suib.toString();
      //这里的id是上面data里面定义的  是上面赋值到data数组里的
      const res = await this.$apis.quedingID(this.id, bb);
      let { meta } = res.data;
      if (meta.status == 200) {
        this.$message.success("更新成功");
        this.dialogVisibless = false;
        this.addForm();
      }
    },
    async del(id, ids) {
      console.log(id, ids);
      const res = await this.$apis.delId({
        ids,
        id
      });
      let { meta } = res.data;
      if (meta.status == 200) {
        this.$ask("确定删除嘛");
        this.addForm();
      }
    }
  }
};
</script>
<style lang="scss">
.Hys_card {
  margin-top: 10px;
}
.cont {
  width: 100%;
  display: flex;
  // height: 500px;
  .left {
    width: 30%;
    // height: 500px;
  }
  .right {
    // flex: 1;
    width: 70%;
    display: flex;
    .right_one {
      width: 300px;
    }
    .right_two {
      flex: 1;
    }
  }
}
</style>
